Ext.onReady(function(){
	/**
	 * 创建
	 */
	function createColumn(obj){
		var oColumn=new Object();
		oColumn.header=obj.header;
		oColumn.dataIndex=obj.dataIndex;
		oColumn.sortable=obj.sortable;
		if(obj.type=="boolean"){
			oColumn.renderer=function(value){
							if(value==1){
								return '支出';
							}else if(value==0){
								return '收入';
							}else{
								return ;
							}
						};
		}
		return oColumn;
	}
	
	function createFormField(obj){
		//debugger;
		if(!obj.formField){
			return null;
		}
		
		var oField=new Object();
		
		if(obj.xtype=="radio"){
			oField=createRadioGroupField(obj);
		}else{
			oField.fieldLabel=obj.fieldLabel;
			oField.allowBlank=obj.allowBlank;
			oField.name=obj.dataIndex;
			oField.xtype=obj.xtype;
			oField.anchor=obj.anchor||'80%';
			if(obj.xtype=="numberfield"){
				oField.allowDecimals=obj.allowDecimals||false;
				oField.maxValue=obj.maxValue;
				oField.minValue=obj.minValue;
			}
		}
				
		return oField;
	}
	/**
	 * 创建radio
	 */
	function createRadioGroupField(obj){
		var radioField={
		    layout:'table',
            autoHeight:true,
		    baseCls : 'x-plain',
		    items:[
		    	{
			   	 	 layout: 'form',
		             baseCls : 'x-plain',
		             cls:'x-form-field',
		             width:100,
		         	 items: [{text: obj.fieldLabel+':' ,baseCls : 'x-plain',xtype:'label'}]}
		    ]
		};
		
		var boxLabels=Ext.decode(obj.boxLabel);

		for(var i=0;i<boxLabels.length;i++){
			var oRadio=boxLabels[i];
			radioField.items.add({
				cls:'x-form-field',
				baseCls : 'x-plain',items: [{baseCls : 'x-plain',boxLabel:oRadio.boxLabel,inputValue:oRadio.value, name:obj.dataIndex,xtype:'radio'}]
			});
		}
		
		return radioField;
		
	}
	
	Ext.Ajax.request({					//调用Ajax发送请求到后台
			//scope:this,
			url:'../hf/gridImpl.do?action=tableInfo&uniqueCode=10066',					//删除grid数据的url.
			success:function(transport){				//处理成功后返回的函数
				var oXmlDom=transport.responseText;

				if(oXmlDom!=''){
					var content=eval("("+oXmlDom+")");
					var obj=content.data;
					
					var cmDataArray=new Array();
					
					var formFields=[{
						name : 'uniqueCode',
						xtype:'hidden'
					}];
					
					for(var i=0;i<obj.gridColumns.length;i++){
						cmDataArray[cmDataArray.length]=createColumn(obj.gridColumns[i]);
						var formField=createFormField(obj.gridColumns[i]);
						if(formField){
							formFields[formFields.length]=formField;
						}						
					}
					
					
				
				var filters = new Ext.grid.GridFilters({
				  filters:obj.gridColumns});
						
				new Ext.ux.GridExtend({
					editUrl:obj.editUrl,
					deleteUrl:obj.deleteUrl,
					listUrl:obj.listUrl,
					formSaveUrl:obj.formSaveUrl,
					el:'grid',
					sm:new Ext.grid.CheckboxSelectionModel({//start Ext.grid.CheckboxSelectionModel
						//选择全部时，只选择私有的分类
						selectAll : function(){
					        if(this.locked) return;
					        this.selections.clear();
					        for(var i = 0, len = this.grid.store.getCount(); i < len; i++){
					        	if(this.grid.store.data.items[i].json.isPublic!='0'){
					        		this.selectRow(i, true);
					        	}		            
					        }
					    },
					    //只属于私有的分类的行才有checkbox
					    renderer : function(v, p, record,rowIndex){
					    	if(record.json.isPublic!='0'){
					    		return '<div class="x-grid3-row-checker">&#160;</div>';
					    	}else{
					    		return ;
					    	}
					    }
					}),
					rowdblclick:function(grid, rowIndex, e){
						var selectId=this.gridStore.data.items[rowIndex].id;
						if(this.gridStore.data.items[rowIndex].json.isPublic=='0'){
							return;
						}
						this.editInfo(selectId);
					},
					formFields:formFields,
					filters:filters,
					cmDataArray:cmDataArray
				}).render();
				}else{
					this.grid.store.reload();		//重新加载grid中的数据
					this.reloadStore();
				}
			},
			failure:function(){				//处理后台删除失败的函数
				Ext.MessageBox.alert('消息','删除失败!');
			}
		});  
});